QRS detection with MatLab script of Hooman Sedghamiz


In [1]:
import wfdb
import numpy as np
import matplotlib.pyplot as plt
import sys

Downloading an ECG from mimic III


In [2]:
folder = "p05/p050140"
waveform = "p050140-2188-07-26-05-51"
sig, fields = wfdb.srdsamp(waveform,pbdir='mimic3wdb/matched/'+folder)

In [3]:
print("signame: " + str(fields['signame']))
print("units: " + str(fields['units']))
print("fs: " + str(fields['fs']))
print("comments: " + str(fields['comments']))
print("fields: " + str(fields))


signame: ['aVR', 'II', 'I', 'III', 'ABP', 'CVP', 'PLETH']
units: ['mV', 'mV', 'mV', 'mV', 'mmHg', 'mmHg', 'NU']
fs: 125
comments: ['Location: micu']
fields: {'units': ['mV', 'mV', 'mV', 'mV', 'mmHg', 'mmHg', 'NU'], 'fs': 125, 'comments': ['Location: micu'], 'signame': ['aVR', 'II', 'I', 'III', 'ABP', 'CVP', 'PLETH']}

Lets check if the signal contains the signal II


In [4]:
signalII = None
try:
    signalII = fields['signame'].index("II")
except ValueError:
    print("List does not contain value")
if(signalII!=None):
    print("List contain value ",signalII)


List contain value  1

Obtain only the signal II from the ECG


In [5]:
array = sig[:, signalII]
array = np.nan_to_num(array)

Save the Frame Secuence


In [6]:
fs = fields['fs']
fs


Out[6]:
125

SubArray 400000


In [18]:
window = 100000
sampfrom = 0
sampto = window
subArray = array[sampfrom:sampto]
print(len(subArray))
print(subArray)


Out[18]:
100000

In [8]:
from oct2py import octave
octave.addpath('/home/scidb/HeartRatePatterns/Matlab/')
octave.eval('pkg load signal')

In [9]:
%load_ext oct2py.ipython

In [10]:
%%octave -i subArray,fs -o R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot
[R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot] = peakdetect(subArray,fs,false);


warning: peakdetect: some elements in list of return values are undefined

In [17]:
Ri = R_i
Ramp = R_amp
Si = S_i
Samp = S_amp
Ti = T_i
Tamp = T_amp
Qi = Q_i
Qamp = Q_amp

In [22]:
sampfrom += window+1
sampto += window
subArray = array[sampfrom:sampto]

In [23]:
%%octave -i subArray,fs -o R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot
[R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot] = peakdetect(subArray,fs,false);


warning: peakdetect: some elements in list of return values are undefined

In [25]:
print("R_i",R_i)
print("Ri",Ri)
print("S_i",S_i)
print("R_i",T_i)
print("Q_i",Q_i)
print(heart_rate)
print(buffer_plot)


R_i [[   22  2961 10563 15655 15735 16110 21135 34302 37092 37313 37880 38017
  38090 39373 40303 40654 40857 40945 42342 43042 43322 45015 45505 46281
  48167 50066 50914 52336 53262 54343 55192 55688 56040 57303 58232 58586
  58842 58933 59119 59273 59414 59536 59628 59767 59972 60174 60456 60669
  61355 61907 62118 62396 64795 69237 71666 71763 71836 71905 71973 72041
  82700 84844 85325 92894 95156 97806 98368 98724]]
Ri [[ 3350  3609  3727  3833  4044  4255  4532  4947  5148  5576  5787  5929
   6139  6345  6560  6757  7329  7816  8452  8589  9079  9285  9640 10057
  10541 10749 10959 11171 11386 11599 11811 12088 12295 12910 13059 13271
  13486 13762 14075 14256 14401 14612 14817 15088 15177 15384 15524 16296
  16504 16713 17199 17480 17619 17921 18174 18374 18923 20983 21550 21900
  22800 23157 23362 24343 24837 25042 25247 25452 25655 25861 26063 26261
  26402 26674 27006 27152 27356 27701 28253 28526 28787 29464 29808 29992
  30189 30594 30793 31699 31895 32222 33145 34074 35028 35133 35239 35387
  35461 35842 36889 37108 37925 38252 38652 39115 39444 39903 40035 40154
  40370 40566 40718 40820 40892 40967 41297 41395 41586 42267 42399 42583
  42714 43275 43399 43522 43595 43663 43782 43969 44332 44421 44517 44610
  44820 44944 45179 45261 45447 45572 45752 45876 46037 46168 46312 46405
  46552 46923 47211 47418 47667 47793 47981 48102 48179 48606 48867 49311
  49504 49817 51213 51468 51938 52193 54405 54721 55038 55199 55623 56284
  56897 59177 59866 60182 60744 61058 62546 62710 64748 66783 67376 67567
  68785 69348 70224 70494 70996 74494 75110 76526 82995 86841 89790 91361
  91691 93719 93864 98698]]
S_i [[  764  5562 13180 15662 16011 16359 30276 35948 37169 37452 37946 38028
  38665 40014 40443 40731 40871 41716 42557 43187 43605 45366 45786 46634
  48586 50496 51196 52693 53768 55051 55478 55901 56111 58084 58442 58653
  58860 59000 59128 59341 59424 59543 59634 59832 60039 60236 60529 60736
  61771 61977 62259 62817 66602 71083 71677 71774 71843 71911 71979 72866
  82983 85190 90445 93320 95776 98232 98580]]
R_i [[ 2925 10526 15644 15673 16043 21097 34264 37056 37272 37823 37980 38052
  39338 40261 40617 40855 40879 42306 43008 43287 44974 45468 46211 48129
  50031 50877 52298 53225 54305 55155 55651 56004 57266 58190 58545 58803
  58868 59070 59241 59356 59584 59748 59907 60114 60402 60629 61291 61848
  62083 62361 64794 69201 71690 71796 71867 71934 72002 82660 84808 85290
  92857 95118 97769 98332 98683]]
Q_i [[   17  2956 10558 15650 15733 16105 21130 34297 37087 37308 37875 38012
  38085 39368 40301 40649 40857 40943 42337 43037 43317 45013 45500 46279
  48162 50061 50909 52331 53257 54338 55187 55683 56035 57298 58230 58584
  58837 58928 59114 59273 59409 59531 59625 59763 59970 60169 60451 60667
  61350 61902 62113 62391 64795 69232 71661 71758 71836 71905 71973 72041
  82695 84839 85320 92889 95154 97801 98363 98719]]
[]
[[ 0.32440945  0.32440945  0.32913386 ...,  0.45826772  0.47716535
   0.49448819]]

In [12]:
To= 5
subarray = buffer_plot[0][Q_i[0][0]:T_i[0][To]]
print(len(subarray))
subQ_i = Q_i[0][:To]
subQ_amp = Q_amp[0][:To]
subR_i = R_i[0][:To]
subR_amp = R_amp[0][:To]
subS_i = S_i[0][:To]
subS_amp = S_amp[0][:To]
subT_i = T_i[0][:To]
subT_amp = T_amp[0][:To]


1145

In [13]:
fig_size = [12,9]
plt.rcParams["figure.figsize"] = fig_size

In [14]:
plt.plot(np.arange(Q_i[0][0],T_i[0][To]),np.array(subarray),c='lightgreen')
plt.scatter(subQ_i, subQ_amp,c='yellow')
plt.scatter(subR_i, subR_amp,c='blue')
plt.scatter(subS_i, subS_amp,c='red')
plt.scatter(subT_i, subT_amp,c='black')
plt.show()



In [29]:
size = 65
sq = S_i[0][:size]-Q_i[0][:size]
sq


Out[29]:
array([ 747, 2606, 2622,   12,  278,  254, 9146, 1651,   82,  144,   71,
         16,  580,  646,  142,   82,   14,  773,  220,  150,  288,  353,
        286,  355,  424,  435,  287,  362,  511,  713,  291,  218,   76,
        786,  212,   69,   23,   72,   14,   68,   15,   12,    9,   69,
         69,   67,   78,   69,  421,   75,  146,  426, 1807, 1851,   16,
         16,    7,    6,    6,  825,  288,  351, 5125,  431,  622])

In [30]:
tq = T_i[0][:size]-Q_i[0][:size]
tq


Out[30]:
array([ 2908,  7570,  5086,    23,   310,  4992, 13134,  2759,   185,
         515,   105,    40,  1253,   893,   316,   206,    22,  1363,
         671,   250,  1657,   455,   711,  1850,  1869,   816,  1389,
         894,  1048,   817,   464,   321,  1231,   892,   315,   219,
          31,   142,   127,    83,   175,   217,   282,   351,   432,
         460,   840,  1181,   733,   459,  2681,  6810,  6895,  2564,
         206,   176,   166, 10755, 12835, 13249, 10162, 10279, 12449,
        5443,  3529])

In [31]:
ts = T_i[0][:size]-S_i[0][:size]
ts


Out[31]:
array([ 2161,  4964,  2464,    11,    32,  4738,  3988,  1108,   103,
         371,    34,    24,   673,   247,   174,   124,     8,   590,
         451,   100,  1369,   102,   425,  1495,  1445,   381,  1102,
         532,   537,   104,   173,   103,  1155,   106,   103,   150,
           8,    70,   113,    15,   160,   205,   273,   282,   363,
         393,   762,  1112,   312,   384,  2535,  6384,  5088,   713,
         190,   160,   159, 10749, 12829, 12424,  9874,  9928,  7324,
        5012,  2907])

In [32]:
heartRate = np.stack((sq, tq,ts), axis=-1)

In [33]:
from sklearn.cluster import KMeans

In [34]:
X = np.array(heartRate)
est = KMeans(n_clusters=10).fit(X)

In [35]:
print(est.labels_)
print(est.cluster_centers_)


[9 2 8 0 0 7 3 9 0 0 0 0 4 0 0 0 0 4 0 0 4 0 0 4 4 0 4 0 4 0 0 0 4 0 0 0 0
 0 0 0 0 0 0 0 0 0 4 4 0 0 9 7 2 9 0 0 0 1 5 5 1 1 6 7 9]
[[   176.62162162    364.32432432    187.7027027 ]
 [   215.          10398.66666667  10183.66666667]
 [  2206.5          7232.5          5026.        ]
 [  9146.          13134.           3988.        ]
 [   344.1          1368.1          1024.        ]
 [   415.5         13042.          12626.5       ]
 [  5125.          12449.           7324.        ]
 [   370.33333333   5748.33333333   5378.        ]
 [  2622.           5086.           2464.        ]
 [  1003.4          2888.2          1884.8       ]]

In [36]:
import collections

In [42]:
labels, values = zip(*collections.Counter(est.labels_).items())
#labels, values = zip(*collections.Counter(patient).most_common(14))
indexes = np.arange(len(labels))
width = 0.5
fig_size = [12,9]
fig, ax = plt.subplots() 
for i, v in enumerate(values):
    ax.text(i-0.1,v+1, str(v), color='blue', fontweight='bold')
    
plt.rcParams["figure.figsize"] = fig_size
plt.bar(indexes, values, width)
plt.xticks(indexes + width * 0.01, labels)
plt.show()



In [ ]:
sampfrom = max(max(R_i[0]),max(S_i[0]),max(T_i[0]),max(Q_i[0]))
sampfrom

In [ ]:
sampfrom = 400000
sampto = 400000+sampfrom
folder = "p05/p050140"
waveform = "p050140-2188-07-26-05-51"
sig, fields = wfdb.srdsamp(waveform,pbdir='mimic3wdb/matched/'+folder,
                           sampfrom=sampfrom, sampto=sampto)

In [ ]:
array = np.nan_to_num(sig[:, signalII])
array = array
array

In [ ]:
%%octave -i array -o R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot
[R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot] = peakdetect(array,125,false);

In [ ]:
print(fs)
print("R_i",R_i)
print("S_i",S_i)
print("R_i",T_i)
print("Q_i",Q_i)
print(heart_rate)
print(buffer_plot)

In [ ]: